import java.util.LinkedList;
import java.util.Random;


public class FloorGenerator 
{
	private Game game;
	private Random random = new Random();
	private Level curLevel;
	public LinkedList<Level> levels= new LinkedList<Level>( );
	public int floorsLeft;
	private int level = 0;
	private int ticks = 0;

	public FloorGenerator(Game g)
	{
		this.game = g;
		levels = Game.gamePlay.getLevels();
		Level l = levels.getFirst();
		System.out.println(l.getNumFloorsDuration() + " " + l.getMinGaps()  + " " + l.getMaxGaps() + " " + l.getFloorsOnScreen()+ " " + l.getInterFloorSpaces(g));
		setNextLevel();
	}

	private void setNextLevel() {

		curLevel = levels.getFirst();
		if(level < 5)
			curLevel.setFloorsSpeed(curLevel.getFloorsSpeed()+0.01);
		else if (level < 10)
			curLevel.setFloorsSpeed(curLevel.getFloorsSpeed()+0.002);
		else 
			curLevel.setFloorsSpeed(curLevel.getFloorsSpeed()+0.001);
		
		floorsLeft = curLevel.getNumFloorsDuration();
		
		Floor.speed = curLevel.getFloorsSpeed();
		level++; ticks = 0;
	}

	public void update()
	{
		//System.out.println(Floor.speed);
		//System.out.println(tog.HEIGHT/(curLevel.getFloorsOnScreen()*Floor.speed));
		if( ticks % curLevel.getInterFloorSpaces(game) == 0 )
		{
			//System.out.println("creating");
			boolean[] gaps = new boolean[Game.gamePlay.getNumBlocks()];
			int numGaps = random.nextInt(curLevel.getMaxGaps()-curLevel.getMinGaps())+curLevel.getMinGaps();

			for(int i=0; i<gaps.length;i++)
			{
				gaps[i]=false;
			}

			while(numGaps > 0)
			{
				int i = random.nextInt(gaps.length);
				if(gaps[i]==false)
				{
					gaps[i]=true;
					numGaps--;
				}
			}
			game.add(new Floor(gaps));
			floorsLeft--; 
			if(floorsLeft<=0)
			{
				setNextLevel();
			}
		}
		ticks++;

	}

	public int getLevel() {
		return level;
	}




}
